package org.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.example.entity.Area;
import org.example.entity.Pub;
import org.example.service.AreaService;
import org.example.service.IPubService;
import org.example.utils.Result;
import org.example.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author scs
 * @since 2025-03-05
 */
@RestController
@CrossOrigin
@RequestMapping("/scs")
@Api(tags = "推荐酒店信息")
public class PubController {
    @Autowired
    private IPubService pubService;

    @Autowired
    private AreaService areaService;

    @ApiOperation("获取热门酒店列表")
    @RequestMapping("/get-list")
    public Result getList(@RequestParam  Integer id){
        List<Pub> pubs = pubService.selectListById(id);
        return new Result("200", "查询成功",pubs);
    }

    @ApiOperation("根据地区和品牌获取酒店列表")
    @RequestMapping("/get-brandList")
    public Result getDetail(@RequestParam String brand, @RequestParam Integer area){
        List<Pub> pubs = pubService.selectListByBrand(area,brand);
        return new Result("200", "查询成功",pubs);
    }

    @ApiOperation("根据城市和品牌,星级获取酒店列表")
    @RequestMapping("/get-areaList")
    public Result getAreaList(@RequestParam(defaultValue = "") String brand, @RequestParam(defaultValue = "") String area, @RequestParam(defaultValue = "") String rank){
        if (!Objects.equals(area, "") && area!=null){
            Area areas = areaService.getOne(new QueryWrapper<Area>().eq("name", area));  //获取起点id
            List<Pub> pubs = pubService.selectList(areas.getId(),brand,rank);
            return new Result("200", "查询成功",pubs);
        }else {
            List<Pub> pubs = pubService.selectList(null,brand,rank);
            return new Result("200", "查询成功",pubs);
        }
    }

    @ApiOperation("根据酒店Id获取酒店详情")
    @RequestMapping("/get-pubDetail")
    public Result getArea(@RequestParam Integer pubId){
        Pub pub = pubService.selectPubById(pubId, Math.toIntExact(ThreadLocalUtil.getCurrentId()));
        //增加浏览量
        Pub pub1 = new Pub();
        pub1.setId(1);
        pub1.setBrowse(pub.getBrowse()+1);
        //判断是否收藏

        boolean result = pubService.updateById(pub1);
        return new Result("200", "查询成功",pub);
    }

    @ApiOperation("根据酒店Id获取酒店房间列表")
    @RequestMapping("/get-pubRoomList")
    public Result getPubRoom(@RequestParam Integer pubId){
        List<Pub> rooms = pubService.getPubRoomByPubId(pubId);
        return new Result("200", "查询成功",rooms);
    }


}
